<?php
/**
* 用户收益模块后台 Service
*
* 用户收益模块后退 Service的注意及说明
* 以下use模块请不要互相伤害
*
* @author    xiecz <xiecz@yunys.com.cn>
* @copyright 2016 QuanZhouYunYiShang Inc.
*/
namespace App\Services\Statistics;

use Log;

use App\Services\Tool\DataBase\SQL;
use App\Services\Tool\GeneralTool;
use App\Services\UserService\UserTool;
use App\Services\Tool\Statistics\IncomeTool;
use App\Services\Tool\Statistics\UserBankTool;
use App\Services\Tool\Pay\UnionPayTool;
use App\Services\Tool\Pay\UnionChinaPay;

class IncomeBackstage{

    function __construct(IncomeTool $incomeTool,
                         UserBankTool $userBankTool,
                         UnionPayTool $unionpayTool){
        $this->incomeTool   = $incomeTool;
        $this->userBankTool = $userBankTool;
        $this->unionTool    = $unionpayTool;
    }

    /**
        申请提现列表
     */
    function apply_withdraw_list($input){
        $where = [];
        foreach($input as $k=>$v){
            if($k == 'card_holder' || $k == 'phone' || $k == 'state')
                $where[$k] = $v;
        }

        $input['per_page'] = empty($input['per_page']) ? 10 : $input['per_page'];

        $lists = $this->incomeTool->apply_withdraw_list_backstage($where,$input);


        return $lists;
    }

    /**
        提现详情
        参数：
            withdraw_id         提现id
     */
    function user_apply_withdraw($input){
        if(!isset($input['withdraw_id'])) return (object)[];

        $result = $this->incomeTool->user_apply_withdraw_backstage($input);

        return $result;
    }

    /**
        点击转账
        参数：
            withdraw_id         提现编号
     */
    function transfer_accounts($input){
        if(!isset($input['withdraw_id'])) return GeneralTool::createResult('','M00002');
        $result = $this->incomeTool->user_apply_withdraw_backstage($input);

        if(!isset($result) || !isset($result->withdraw_id)) return GeneralTool::createResult('','M05042');
        //判断提现单是否可提现
        if($result->state == 1 && $result->result == 0 && $result->is_over_withdraw == 0){
            // $result = $this->unionTool->transfer_accounts(['order_id'   => $result->withdraw_number,
            //                                                'total_fee'  => $result->total_fee,
            //                                                'accNo'      => $result->bank_card_no,
            //                                                'customerNm' => $result->card_holder]);
            $net = new UnionChinaPay();
            $balance = $net->get_balance();
            Log::info($balance);
            if($balance < $result->total_fee){
                return ['state' => 'M00001','result' => '', 'message' => '储备金不足'];
            }
            $trade_no = "00" . date('YmdHis');
            $union  = $net->test(['order_id'   => $result->withdraw_number,
                                  'merSeqId'   => $trade_no,
                                  'total_fee'  => $result->total_fee,
                                  'bank_name'  => $result->bank_name,
                                  'accNo'      => $result->bank_card_no,
                                  'customerNm' => $result->card_holder,
                                  'prov'       => $result->prov,
                                  'city'       => $result->city,
                                  'purpose'    => 'Mytee提现(用户编号:' . $result->user_id .',提现金额:' . $result->total_fee . ' )']);
            if($union['state'] == '0000'){

                // $re = $this->incomeTool->transfer_accounts_after(['withdraw_number' => $result->withdraw_number,
                //                                                   'trade_no'        => $result->withdraw_number]);
                $re = $this->incomeTool->transfer_accounts_notify(['withdraw_number' => $result->withdraw_number,
                                                                   'trade_no'        => $trade_no]);
                return GeneralTool::createResult('','M00000');
            }
            if($union['state'] == '0104'){
                $union['message'] = '银行卡信息不匹配';
            }
            $re = $this->incomeTool->transfer_accounts_fail(['withdraw_number' => $result->withdraw_number,
                                                             'result_message'  => $union['message'],
                                                             'user_id'         => $result->user_id,
                                                             'total_fee'       => $result->total_fee,
                                                             'poundage'        => $result->poundage]);
            if($re != 'M00000') return GeneralTool::createResult('',$re);
            return ['state' => 'M00001', 'message' => $union['message']];
        }
        return GeneralTool::createResult('','M05043');
    }

    /**
     * 取消提现
     * 参数：
            withdraw_id         提现编号
     */
    function cancel($input){
        if(!isset($input['withdraw_id'])) return GeneralTool::createResult('','M00002');
        $result = $this->incomeTool->user_apply_withdraw_backstage($input);

        if(!isset($result) || !isset($result->withdraw_id)) return GeneralTool::createResult('','M05042');
        $re = $this->incomeTool->transfer_accounts_fail(['withdraw_number' => $result->withdraw_number,
                                                         'result_message'  => '提现失败',
                                                         'user_id'         => $result->user_id,
                                                         'total_fee'       => $result->total_fee,
                                                         'poundage'        => $result->poundage]);
        return GeneralTool::createResult('',$re);
    }

    function test($input){

        return $input;
    }

}
